package org.threebitsoftware.tbb.Contact_GUI;

import java.util.HashMap;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.CLabel;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.threebitsoftware.tbb.dao.*;
import org.threebitsoftware.tbb.engine.EngineUtil;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;

public class AddContactMenu_GUI extends Dialog {

	// ArrayList<Character> testArray = new ArrayList<Character>();
	private static Logger logger = Logger.getLogger(AddContactMenu_GUI.class);
	
	Vector<PhoneNumber> telephoneList_ = new Vector<PhoneNumber>();
	Vector<EmailAddress> emailList_ = new Vector<EmailAddress>();
	Vector<Address> addressList_ = new Vector<Address>();
	private String firstName_ = "";
	private String lastName_ = "";
//	private String city_ = "";
//	private String state_ = "";
//	private String zipCode_ = "";
//	private String country_="";


	// Store the information entered in the text boxes.
	HashMap<String, String> textFieldInfo = new HashMap<String, String>();


	int fieldEnteredCounter_ = 0;

	protected Object result_;
	public Shell shlNewContact_;
	private Text firstNameText_;
	private Text lastNameText_;
	private Text addressText_;
	private Text cityText_;
	private Text stateText_;
	private Text zipCodeText_;
	private Text countrytext_;
	private Text emailText_;
	private Text telephoneText_;
	
	private Combo addressCombo_;
	private Combo telephoneCombo_;
	private Combo emailCombo_;
	
	private Button btnSubmit_;
	
	private Contact cContact_ = null;

	public void setContact(Contact myContact)
	{
		cContact_ = myContact;
	}
	
	public Vector<PhoneNumber> getTelephoneList_() {
		return telephoneList_;
	}

	public void setTelephoneList_(Vector<PhoneNumber> telephoneList) {
		telephoneList_ = telephoneList;
	}

	public Vector<EmailAddress> getEmailList_() {
		return emailList_;
	}

	public void setEmailList_(Vector<EmailAddress> emailList) {
		emailList_ = emailList;
	}

	public Vector<Address> getAddressList_() {
		return addressList_;
	}

	public void setAddressList_(Vector<Address> addressList) {
		addressList_ = addressList;
	}

	public HashMap<String, String> getTextFieldInfo() {
		return textFieldInfo;
	}

	public void setTextFieldInfo(HashMap<String, String> textFieldInfo) {
		this.textFieldInfo = textFieldInfo;
	}

	
	public String getFirstName_() {
		return firstName_;
	}

	public void setFirstName_(String firstName) {
		firstName_ = firstName;
	}

	public String getLastName_() {
		return lastName_;
	}

	public void setLastName_(String lastName) {
		lastName_ = lastName;
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public AddContactMenu_GUI(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlNewContact_.open();
		shlNewContact_.layout();
		Display display = getParent().getDisplay();
		while (!shlNewContact_.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result_;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlNewContact_ = new Shell(getParent(), SWT.DIALOG_TRIM
				| SWT.APPLICATION_MODAL);
		shlNewContact_.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		shlNewContact_.setSize(642, 351);
		shlNewContact_.setText("TBB New Contact");
		
		{
			firstNameText_ = new Text(shlNewContact_, SWT.BORDER);
			firstNameText_.setText(firstName_);
			firstNameText_.setBounds(86, 10, 184, 19);
		}
		{
			CLabel lblFirstName = new CLabel(shlNewContact_, SWT.NONE);
			lblFirstName.setFont(SWTResourceManager.getFont("Tahoma", 10,
					SWT.NORMAL));
			lblFirstName.setBounds(10, 10, 71, 19);
			lblFirstName.setText("First Name");
		}
		{
			CLabel lblLastName = new CLabel(shlNewContact_, SWT.NONE);
			lblLastName.setFont(SWTResourceManager.getFont("Tahoma", 10,
					SWT.NORMAL));
			lblLastName.setBounds(10, 35, 71, 19);
			lblLastName.setText("Last Name");
		}
		{
			lastNameText_ = new Text(shlNewContact_, SWT.BORDER);
			lastNameText_.setText(lastName_);
			lastNameText_.setBounds(86, 35, 184, 19);
		}
		
		//------------------------------------------------------>
		{// Submit Button
			btnSubmit_ = new Button(shlNewContact_, SWT.NONE);
			btnSubmit_.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					
					System.out.println(firstNameText_.getText());
					System.out.println(lastNameText_.getText());
					
					Vector c1Tags = new Vector();
					TagForContact lTag = new TagForContact();
					lTag.setSTag("Default");
					c1Tags.add(lTag);
					
					//Create the contact and add it to the database.
					Contact contact1 = null;
					if(cContact_ == null)
					{//Contact does not exist. Adding new Contact
						
					 contact1 = new Contact(firstNameText_.getText(),
							lastNameText_.getText(), "strMiddleName",
							"strSuffix", "strPrefix", "strCompany", "strTitle",
							true);
						contact1.setTags(c1Tags);
						contact1.setAddresses(addressList_);
						contact1.setEmails(emailList_);
						contact1.setPhoneNumbers(telephoneList_);
						contact1.setIdAppUser(UserProfileFormGUI.getUsersLoginID_());
						logger.info("Added contact " + contact1.toString());
					}
					else
					{
						 contact1 = new Contact(firstNameText_.getText(),
									lastNameText_.getText(), "strMiddleName",
									"strSuffix", "strPrefix", "strCompany", "strTitle",
									true);
							contact1.setTags(c1Tags);
							contact1.setAddresses(addressList_);
							contact1.setEmails(emailList_);
							contact1.setPhoneNumbers(telephoneList_);
							contact1.setIdContact(cContact_.getIdContact());
							logger.info("Updated contact " + contact1.toString());
							contact1.setIdAppUser(UserProfileFormGUI.getUsersLoginID_());
//						contact1 = cContact_;
					}
					
					//Engine will add the Contact to the database.
					// Add the contact info in the DB.
					EngineUtil.storeContact(contact1);
					
					//The contact which is added to the database. 
					System.out.print("\n" + contact1.toString());
					
//					ContactFormGUI cfg = new ContactFormGUI();
					shlNewContact_.close();
				}
			});
			btnSubmit_.setBounds(533, 298, 93, 23);
			btnSubmit_.setText("&Submit");
		}

		//Address List
		{

			Group grpSadad = new Group(shlNewContact_, SWT.NONE);
			grpSadad.setBounds(10, 60, 616, 119);
			{
				CLabel label = new CLabel(grpSadad, SWT.NONE);
				label.setFont(SWTResourceManager.getFont("Tahoma", 10,
						SWT.NORMAL));
				label.setBounds(10, 25, 71, 19);
				label.setText("Address");
			}
			{
				addressText_ = new Text(grpSadad, SWT.BORDER);
				
				if (!addressList_.isEmpty()){
					
				addressText_.setText(addressList_.get(0).getStreet1());
				}
				addressText_.setBounds(81, 25, 232, 19);
			}
			{
				Label lblCity = new Label(grpSadad, SWT.NONE);
//				lblCity.setText(city_);
				lblCity.setFont(SWTResourceManager.getFont("Tahoma", 10,
						SWT.NORMAL));
				lblCity.setBounds(10, 82, 71, 16);
				lblCity.setText("City");
			}
			{
				cityText_ = new Text(grpSadad, SWT.BORDER);
				if (!addressList_.isEmpty()){
					
					cityText_.setText(addressList_.get(0).getCity());
					}
				cityText_.setBounds(81, 79, 104, 19);
			}
			{
				Label lblState = new Label(grpSadad, SWT.NONE);
//				lblState.setText(state_);
				lblState.setFont(SWTResourceManager.getFont("Tahoma", 10,
						SWT.NORMAL));
				lblState.setBounds(197, 82, 92, 16);
				lblState.setText("State/Province");
			}
			{
				Label lblZipCode = new Label(grpSadad, SWT.NONE);
//				lblZipCode.setText(zipCode_);
				lblZipCode.setFont(SWTResourceManager.getFont("Tahoma", 10,
						SWT.NORMAL));
				lblZipCode.setBounds(377, 82, 54, 16);
				lblZipCode.setText("Zip Code");
			}
			{
				stateText_ = new Text(grpSadad, SWT.BORDER);
				if (!addressList_.isEmpty()){
					
					stateText_.setText(addressList_.get(0).getProvince());
					}
				
				stateText_.setBounds(295, 81, 76, 19);
			}
			{
				zipCodeText_ = new Text(grpSadad, SWT.BORDER);
				
				if (!addressList_.isEmpty()){
					
					zipCodeText_.setText(addressList_.get(0).getZipCode());
					}

				zipCodeText_.setBounds(437, 81, 76, 19);
			}
			
			//Add Address Button------------------------------------------------>
			{
				Button addressAddButton = new Button(grpSadad, SWT.NONE);
				addressAddButton.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {

						//Add the addresses for the contacts.
						Address address1 = new Address(addressText_.getText(),
								"strStreet2", "strStreet3", countrytext_.getText(),
								zipCodeText_.getText(), stateText_.getText(), cityText_
										.getText(), addressCombo_.getText());
						
						addressList_.add(address1);
						String contactAddressInformation = addressText_
								.getText()
								+ " "
								+ cityText_.getText()
								+ " "
								+ stateText_.getText()
								+ " "
								+ zipCodeText_.getText()
								+ " "
								+ addressCombo_.getText();

						System.out.print("Added Contact: "
								+ contactAddressInformation);
						
						//After the address is added Clear the previous entries from the text box.
						addressText_.setText("");
						cityText_.setText("");
						stateText_.setText("");
						zipCodeText_.setText("");
						countrytext_.setText("");
						addressCombo_.select(0);
						
					}
				});
				addressAddButton.setBounds(538, 23, 68, 22);
				addressAddButton.setText("+");
			}
			{
				addressCombo_ = new Combo(grpSadad, SWT.NONE);
				addressCombo_.setItems(new String[] {"Residence", "Work"});
				
				if (!addressList_.isEmpty()){
					System.out.println("addressList is not empty.");
					String addressType = addressList_.get(0).getDescription();
					setDropItemByIndex(addressType, addressCombo_);
				} else {
					addressCombo_.select(0);
				}
				addressCombo_.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.println("In address drop down. ");

					}
				});
				addressCombo_.setBounds(439, 23, 92, 21);
			}

			countrytext_ = new Text(grpSadad, SWT.BORDER);
			countrytext_.setBounds(81, 50, 136, 19);
			if (!addressList_.isEmpty()){
				
				countrytext_.setText(addressList_.get(0).getCountry());
				}
			
			Label lblCountry = new Label(grpSadad, SWT.NONE);
			lblCountry.setFont(SWTResourceManager.getFont("Tahoma", 10,
					SWT.NORMAL));
			lblCountry.setBounds(10, 50, 71, 22);
			lblCountry.setText("Country");
		}

		//Email List
		{
			Group group = new Group(shlNewContact_, SWT.NONE);
			group.setBounds(10, 243, 616, 49);
			{
				emailText_ = new Text(group, SWT.BORDER);
				if (!emailList_.isEmpty()){
					
					emailText_.setText(emailList_.get(0).getEmailAddress());
					}
				emailText_.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {

					}
				});
				emailText_.setBounds(124, 20, 184, 19);
			}
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setBounds(10, 20, 108, 19);
				label.setText("Email ");
			}
			
			{//Add Email's Button-------------------------------------->
				
				Button emailAddbutton = new Button(group, SWT.NONE);
				emailAddbutton.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						
						EmailAddress contactEmail = new EmailAddress(emailText_
								.getText(), emailCombo_.getText());
						emailList_.add(contactEmail);
						System.out.println("Added Email " + contactEmail.toString());
						
						//Reset the previous entries after the new Email has been added. 
						emailText_.setText("");
						emailCombo_.select(0);

					}
				});
				emailAddbutton.setBounds(538, 18, 68, 22);
				emailAddbutton.setText("+");
			}
			{
				emailCombo_ = new Combo(group, SWT.NONE);
				emailCombo_.setItems(new String[] { "Personal", "Work",
						"Random" });
							
				if (!emailList_.isEmpty()){
					System.out.println("Email list is not empty.");
					String emailType = emailList_.get(0).getDescription();
//					emailCombo_.setItems(new String[] { "Residence", "Work",
//					"Cellular" });
					setDropItemByIndex(emailType, emailCombo_);
//					telephoneCombo_.setBounds(440, 21, 92, 22);
				} else {
					emailCombo_.select(0);
				}
				
				emailCombo_.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.print("\nIn Email drop down. ");
					}
				});

				emailCombo_.setBounds(439, 20, 92, 21);

			}
		}

		//Telephone List
		{
			Group group = new Group(shlNewContact_, SWT.NONE);
			group.setBounds(10, 185, 616, 52);
			{
				CLabel label = new CLabel(group, SWT.NONE);
				label.setBounds(10, 23, 108, 19);
				label.setText("Telephone#");
			}
			{
				telephoneText_ = new Text(group, SWT.BORDER);
				if (!telephoneList_.isEmpty()){
					
					telephoneText_.setText(telephoneList_.get(0).getPhoneNumber());
					}
				telephoneText_.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.print("I am in telephone text...");

					}
				});
				telephoneText_.setBounds(124, 23, 185, 19);
			}
			
			{//Telephone Add button ------------------------------------->
				Button telephoneAddButton = new Button(group, SWT.NONE);
				telephoneAddButton.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.println("In telephone Add Button");
						PhoneNumber contactPhoneNumber = new PhoneNumber(
								telephoneText_.getText(), telephoneCombo_
										.getText());
						telephoneList_.add(contactPhoneNumber);
						System.out.println("Added phone: " + contactPhoneNumber.toString());						
						telephoneText_.setText("");
						telephoneCombo_.select(0);
					}
				});
				telephoneAddButton.setBounds(538, 21, 68, 22);
				telephoneAddButton.setText("+");
			}
			{
				telephoneCombo_ = new Combo(group, SWT.NONE);
				telephoneCombo_.setItems(new String[] { "Residence", "Work",
				"Cellular" });
				
				if (!telephoneList_.isEmpty()){
					System.out.println("Telephone list is not empty.");
					String telephoneType = telephoneList_.get(0).getDescription();

					setDropItemByIndex(telephoneType, telephoneCombo_);
					telephoneCombo_.setBounds(440, 21, 92, 22);
				} else {
					telephoneCombo_.select(0);
				}
				telephoneCombo_.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.print("In Telephone drop down. ");
					}
				});
				telephoneCombo_.setBounds(439, 20, 92, 21);
		
			}
		}
		
		//Cancel ------------------------------------------------->
		{
			Button btnCancel = new Button(shlNewContact_, SWT.NONE);
			btnCancel.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					System.out.print("\n Cancel button is clicked. This should reset the form....");				
					shlNewContact_.close();
				}
			});
			btnCancel.setBounds(430, 298, 93, 23);
			btnCancel.setText("&Cancel");
		}

	}
	
	public void setDropItemByIndex(String dropItemString, Combo dropDownObject){
		if (dropItemString==null){
			dropDownObject.select(0);
		}else if ((dropItemString!=null)){
			
		
		for (int i=0; i<dropDownObject.getItemCount(); i++){
			
			if (dropItemString.equals(dropDownObject.getItem(i))){
				dropDownObject.select(i);
				break;
			} else {
				dropDownObject.select(0);
			}
		}

	}
}
	
}
